package Controller;
 

 
import java.util.Date;
 

 
import javax.swing.JFrame;
 
import javax.swing.JOptionPane;
 
import javax.swing.JPanel;
 

 
import Entity.CasualSubscription;
 
import Entity.Customer;
 
import Entity.FullSubscription;
 
import Entity.OneTimeCostumerOrder;
 
import Entity.Order;
 
import Entity.Prices;
 
import GUI.CheckoutGui;
 
import GUI.DisableSaveActivatePlSpotGui;
 
import GUI.FrameSystem;
 
import GUI.ISMainGui;
 
import GUI.InformationStationSubscriptionGui;
 
import GUI.InitParkingLotGui;
 

 
/**
 
 * this is the controller of the parking lot worker. it will mainly run the window options of car checkout, initialize the parking lot and disable/save/activate the parking spot.
 
 * @author Or Marx
 
 *
 
 */
 
public class ParkingLotWorkerController {
 

 
        public void disableSaveActivatePspot() {
 
                JFrame frame=FrameSystem.getFrame();
 
                DisableSaveActivatePlSpotGui plwGui=new DisableSaveActivatePlSpotGui();
 
                plwGui.setLastPanel((JPanel)frame.getContentPane());
 
                plwGui.setFrame(frame);
 
                frame.setContentPane(plwGui);
 
                frame.setVisible(true);
 
        }
 

 
        public void initParkingLot() {
 
                JFrame frame=FrameSystem.getFrame();
 
                InitParkingLotGui plwGui=new InitParkingLotGui();
 
                plwGui.setLastPanel((JPanel)frame.getContentPane());
 
                plwGui.setFrame(frame);
 
                frame.setContentPane(plwGui);
 
                frame.setVisible(true);
 
        }
 
        
 
        public void carCheckout() {
 
                JFrame frame=FrameSystem.getFrame();
 
                CheckoutGui plwGui=new CheckoutGui();
 
                plwGui.setLastPanel((JPanel)frame.getContentPane());
 
                plwGui.setFrame(frame);
 
                frame.setContentPane(plwGui);
 
                frame.setVisible(true);
 
        }
 
        
 
        
 
        
 
        public static boolean carCheckout(String carNum) throws Exception
 
        {
 
                int kind = 0;   //kind of customer (subscriber/one time...)
 
                int index = 0;  //index in the array
 
                Date curTime=new Date();
 
                FullSubscription [] fullSubs=Client.getFullSubscriptions();     //kind 1
 
                CasualSubscription [] casualSubs=Client.getCasualSubscriptions();       //kind 2
 
                OneTimeCostumerOrder [] oneTimeCustomerOrders=Client.getOneTimeCustomerOrders();        //kind 3
 
                Order [] orders=Client.getOrders();     //kind 4
 
                
 
                for (int i=0; i<fullSubs.length; i++)
 
                {
 
                        if (fullSubs[i].getCar_number().equals(carNum) && fullSubs[i].getStart_date().before(curTime) && fullSubs[i].getEnd_date().after(curTime) )
 
                        {
 
                                kind=1;
 
                                index=i;
 
                                break;
 
                        }
 
                }
 
                for (int i=0; i<casualSubs.length; i++)
 
                {
 
                        if (casualSubs[i].getCar_number().equals(carNum) && casualSubs[i].getStart_date().compareTo(curTime)<0 && casualSubs[i].getEnd_date().compareTo(curTime)>0 && casualSubs[i].getParking_lot_number()==Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum())
 
                        {
 
                                kind=2;
 
                                index=i;
 
                                break;
 
                        }
 
                }
 
                for (int i=0; i<oneTimeCustomerOrders.length; i++)
 
                {
 
                        if (oneTimeCustomerOrders[i].getCar_number().equals(carNum) && oneTimeCustomerOrders[i].getEntering_hour().compareTo(curTime)<0 && oneTimeCustomerOrders[i].getActual_leaving_hour()==null && oneTimeCustomerOrders[i].isPayment_status()==false)
 
                        {
 
                                kind=3;
 
                                index=i;
 
                                break;
 
                        }
 
                }
 
                for (int i=0; i<orders.length; i++)
 
                {
 
                        if(orders[i].isCanceled()==false && 
                        		orders[i].getCarNumber().equals(carNum) &&
                        		orders[i].getParkingLotNumber()==0/*Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum()*/ && 
                        		orders[i].getActualArrivalDate()!=null && orders[i].getActualLeavingDate()==null)
 
                        {
 
                                kind=4;
 
                                index=i;
 
                                break;
 
                        }
 
                }
 
                if(Client.getParkingLots_sorted()[0/*Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum()-1*/].popCar(carNum))
 
                        switch(kind)
 
                        {
 
                        case 1: //full subscription
 
                                if (fullSubs[index].getEnd_date().before(new Date()))
 
                                        fullSubs[index].setPenalty(1);
 
                                JOptionPane.showMessageDialog(null,"this car is a subscriber, dont take money","full subscription checkout",JOptionPane.INFORMATION_MESSAGE);
 
                                break;
 
                        case 2: //casual subscription
 
                                JOptionPane.showMessageDialog(null,"this car is a subscriber, dont take money","casual subscription checkout",JOptionPane.INFORMATION_MESSAGE);
 
                                break;
 
                        case 3: //one time customer order
 
                                float payment=0;
 
                                final Prices p=Client.getCurrentPrices(Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum());
 
                                oneTimeCustomerOrders[index].setActual_leaving_hour(new Date());
 
                                oneTimeCustomerOrders[index].setPayment_status(true);
 
                                payment+=((oneTimeCustomerOrders[index].getActual_leaving_hour().getTime() - oneTimeCustomerOrders[index].getEntering_hour().getTime())/(1000*60*60))*p.getOccasionalParkingPlace();
 
                                oneTimeCustomerOrders[index].setPayment(payment);
 
                                JOptionPane.showMessageDialog(null,"this car is an occational customer.\ncharge "+payment+"$ in cash.","one time costumer checkout",JOptionPane.INFORMATION_MESSAGE);
 

 
                                break;
 
                        case 4: //regular order
 
                                int pay=0;
 
                                final Prices p1=Client.getCurrentPrices(Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum());
 
                                Customer customers[]=Client.getCustomers();
                                /*
                                for (int i=0; i<customers.length; i++)
                                	System.out.println(customers[i].getcustomer_id());
                                */
                                
                                Customer customer = null;
 
                                for (int i=0; i<orders.length; i++)
                                	System.out.println(orders[i].getCarNumber() + " "+ index);
 
                                
                                for (int i=0; i<customers.length; i++)
 
                                        if (customers[i].getcustomer_id().equals(orders[index].getCustomerID()))
 
                                                customer=customers[i];
 
                                orders[index].setActualLeavingDate(new Date());
 
 //                               if (orders[index].getActualLeavingDate().compareTo(orders[index].getLeavingDate())>0)   //its late to exit so charge by higher rate
 
 //                                       pay+=((orders[index].getLeavingDate().getTime() - orders[index].getArrivalDate().getTime())/(1000*60*60))*p1.getOccasionalParkingPlace();
 
                             //   else    //give him back the money if its early (by reguler rate)
 
                                        pay+=((orders[index].getLeavingDate().getTime() - orders[index].getArrivalDate().getTime())/(1000*60*60))*p1.getOrderedParkingPlace();
 
                                customer.AddBalance(-pay);
                                
                                //
                                Client.saveCustomerToDB(customer);
                                //
 
                                JOptionPane.showMessageDialog(null,"this car is a one time ordered customer.\n"+pay+"$ was charged in the system.\nlet him go!","one time ordered checkout",JOptionPane.INFORMATION_MESSAGE);
 

 
                                break;
 
                        case 0:
 
                                JOptionPane.showMessageDialog(null,"checkin problem- check for order history","problem",JOptionPane.INFORMATION_MESSAGE);
 

 
                                break;
 
                        }
 
                else
 
                {
 
                        JOptionPane.showMessageDialog(null,"there is a problem finding this car, call johnson for tech support","error",JOptionPane.ERROR_MESSAGE);
 
                        throw new Exception ("The car were not found");
 
                }
 
                return true;
 
        }
 
        
 

 
}
 
